package com.back.Controller;

import com.back.Dao.DeviceData;
import com.back.Dao.R;
import com.back.Service.DeviceDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DeviceDataController {
    @Autowired
    DeviceDataService deviceDataService;
    // 测试通过
    @GetMapping("/deviceData")
    public R findAllDeice()
    {
        List<DeviceData> devicedata = deviceDataService.findAllDeice();
        if(!devicedata.isEmpty())
            return R.success(devicedata);
        else
            return R.error("未查询到信息");
    }

    @GetMapping("/deviceData/{id}")
    public R findDeviceById(@PathVariable Integer id) {
        List<DeviceData> devicedatas = deviceDataService.findDeviceById(id);
        if(devicedatas != null)
            return R.success(devicedatas);
        else
            return R.error("未查询到此id",devicedatas);
    }

    @DeleteMapping("/deviceData/{id}")
    public R DelDevice(@PathVariable int id) {
        Integer result = deviceDataService.DelDevice(id);
        if(result == 0)
            return R.error("删除失败，请勿重复删除");
        return R.success(result);
    }

    @PostMapping("/deviceData")
    public R addDevice(@RequestBody DeviceData devicedata) {
        deviceDataService.addDeviceData(devicedata);
        return R.success("插入成功");
    }

    @PostMapping("/updatedevicedata")
    public R updateDevice(@RequestBody DeviceData devicedata) {
        deviceDataService.updateDeviceData(devicedata);
        return R.success("修改成功");
    }

}
